package com.hzjh.h5mall.ui.wallet

import android.graphics.Paint
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.hzjh.base.ext.frame.JHBaseActivity
import com.hzjh.base.ext.route.RouteUrl
import com.hzjh.base.frame.adapter.MutableAdapter
import com.hzjh.base.frame.adapter.SimpleGapItemDecor
import com.hzjh.base.router.ext.navigationTo
import com.hzjh.base.style.R
import com.hzjh.base.style.view.loading.LoadingDialogManager
import com.hzjh.base.style.weight.GlideUtils.loadCircleCorp
import com.hzjh.base.style.weight.ToastKt.toastCenter
import com.hzjh.base.utils.function.dp2px
import com.hzjh.base.utils.function.gotoActivityForResult
import com.hzjh.base.utils.other.CheckUtils
import com.hzjh.base.utils.other.CommUtils
import com.hzjh.h5mall.adapter.wallet.RechargeMoneyVH
import com.hzjh.h5mall.databinding.ActivityRechargeBinding
import com.hzjh.h5mall.databinding.ItemRechargeMoneyBinding
import com.hzjh.h5mall.model.wallet.WalletVM
import com.hzjh.service.metamask.MetaMaskUtils

//充值页面
@Route(path = RouteUrl.RECHARGE_ACTIVITY)
class RechargeActivity : JHBaseActivity<ActivityRechargeBinding,WalletVM>() {
    @JvmField
    @Autowired(name = RouteUrl.PARAMS_BALANCE)
    var mBalance = ""


    private val rechargeMoneyAdapter by lazy {
        MutableAdapter(viewModel.rechargeMoneyList.value!!).apply {
            addVhDelegate {
                RechargeMoneyVH(ItemRechargeMoneyBinding.inflate(layoutInflater, it, false)){rechargeMoney->
                    //充值
                    viewBinding.etMoney.setText(rechargeMoney.money.toString())
                }
            }
        }
    }

    override fun initData() {
        LoadingDialogManager.get().showLoading(this)
        viewModel.initMetaMask()
        viewBinding.tvBlance.text=CommUtils.getString(com.hzjh.base.ext.R.string.jh_balance_tip2,mBalance)
        viewModel.reqRechargeQrCode()
        viewModel.reqUserInfo()
        viewModel.getRechargeMoneyList()
        viewModel.reqEthRate()
    }

    override fun initView() {
        viewBinding.etMoney.apply {
            setBackgroundResource(R.color.coreTransparent)
            textSize=32f
            setPadding(0, 0, 0, 0)
        }
        viewBinding.tvPayOther.apply {
            paint.flags = Paint.UNDERLINE_TEXT_FLAG; //下划线
            paint.isAntiAlias = true;//抗锯齿
            setOnClickListener {
                //其他充值方式
                val bundle=Bundle()
                bundle.putString(RouteUrl.PARAMS_BALANCE,mBalance)
                this@RechargeActivity.gotoActivityForResult(clazz =RechargeQrcodeActivity::class.java, flag=-1, bundle = bundle ){
                    finish()
                }
            }
        }
        viewBinding.rvRecharge.layoutManager = GridLayoutManager(this,3)
        viewBinding.rvRecharge.adapter=rechargeMoneyAdapter
        viewBinding.rvRecharge.addItemDecoration(
            SimpleGapItemDecor(verGap = 12.dp2px())
        )

        viewBinding.btnPayNow.setOnClickListener {
            //metamask 充值
            val money=viewBinding.etMoney.text.toString()
            if(CheckUtils.isEmpty(money)){
                toastCenter(CommUtils.getString(com.hzjh.base.ext.R.string.jh_input_money_tip))
                return@setOnClickListener
            }
            if(CheckUtils.isEmpty(viewModel.ethRate.value)){
                toastCenter(CommUtils.getString(com.hzjh.base.ext.R.string.jh_get_eth_rate_failed))
                return@setOnClickListener
            }
            viewModel.payInfoBean.value?.wallet?.let {
                // 10^-18 以太币 //0x5AF3107A4000 默认0.01个币
                viewModel.metamaskRechargeMoney=money
                viewModel.reqPay(it, MetaMaskUtils.getInstance()?.changeMoneyToHex(money,viewModel.ethRate.value)?:"0x5AF3107A4000")
            }
        }

    }

    override fun initObservable() {
        super.initObservable()
        viewModel.userInfoBean.observe(this){
            viewBinding.ivHead.loadCircleCorp(it.avatar?:"")
            viewBinding.tvName.text=it.email
        }
        viewModel.rechargeMoneyList.observe(this){
            rechargeMoneyAdapter.notifyDataSetChanged()
        }
        viewModel.addPayIdResult.observe(this){
            if(!CheckUtils.isEmpty(it)){
                //提交交易码成功
                navigationTo(RouteUrl.WALLET_DETAIL_ACTIVITY){
                    withString(RouteUrl.PARAMS_WALLET_ID,it)
                }
                finish()
            }else{
                //充值失败
                toastCenter(CommUtils.getString(com.hzjh.base.ext.R.string.jh_recharge_failed))
            }
        }
        viewModel.ethRate.observe(this){
            //获取到汇率后再结束loading
            LoadingDialogManager.get().dismissLoading()
        }
    }

    override fun initViewBinding(): ActivityRechargeBinding {
        return ActivityRechargeBinding.inflate(layoutInflater)
    }
}